/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于jv-rover系统.
 */
package com.pitop.device.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.DeviceGroupException;
import com.pitop.device.dao.DeviceGroupDAO;
import com.pitop.device.entity.DeviceGroup;
import com.pitop.device.entity.DeviceGroupState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("deviceGroupSV")
@Slf4j
public class DeviceGroupSVImpl extends BaseSVImpl<DeviceGroup, Long> implements DeviceGroupSV {

    @Autowired
    private DeviceGroupDAO deviceGroupDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return deviceGroupDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(DeviceGroup entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象DeviceGroup
     *
     * @param id * @param groupCode 组编码
     * @return DeviceGroup
     */
    @Override
    public DeviceGroup load(java.lang.Long id, java.lang.String groupCode) {
        if (id == null && groupCode == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        return deviceGroupDAO.load(param);
    }

    /**
     * 加载一个对象DeviceGroup 通过id
     *
     * @param id
     * @return DeviceGroup
     */
    @Override
    public DeviceGroup loadById(java.lang.Long id) {
        if (id == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceGroupDAO.loadById(id);
    }

    /**
     * 加载一个对象DeviceGroup 通过groupCode
     *
     * @param groupCode 组编码
     * @return DeviceGroup
     */
    @Override
    public DeviceGroup loadByGroupCode(java.lang.String groupCode) {
        if (groupCode == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceGroupDAO.loadByGroupCode(groupCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 DeviceGroup 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, DeviceGroupState newState, DeviceGroupState... oldStates) {
        if (id == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceGroupDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键groupCode,oldStates 共同更新 DeviceGroup 的状态到newState状态
     *
     * @param groupCode 组编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByGroupCode(java.lang.String groupCode, DeviceGroupState newState, DeviceGroupState... oldStates) {
        if (groupCode == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceGroupDAO.updateStateByGroupCode(groupCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 DeviceGroup 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, DeviceGroupState state) {
        if (id == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceGroupDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键groupCode 更新 DeviceGroup 的状态到另一个状态
     *
     * @param groupCode 组编码
     * @param state     状态
     */
    @Override
    public void updateByGroupCode(java.lang.String groupCode, DeviceGroupState state) {
        if (groupCode == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceGroupDAO.updateByGroupCode(groupCode, state, new Date());
    }


    /**
     * 删除对象DeviceGroup
     *
     * @param id * @param groupCode 组编码
     * @return DeviceGroup
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String groupCode) {
        if (id == null && groupCode == null) {
            throw new DeviceGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        deviceGroupDAO.delete(param);
    }


    /**
     * 查询DeviceGroup分页
     *
     * @param deviceGroup 对象
     * @param offset      查询开始行
     * @param limit       查询行数
     * @return List<DeviceGroup>
     */
    @Override
    public List<DeviceGroup> list(DeviceGroup deviceGroup, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (deviceGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(deviceGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return deviceGroupDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(DeviceGroup deviceGroup) {
        Map<String, Object> map = null;
        if (deviceGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(deviceGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return deviceGroupDAO.count(map);
    }

    /**
     * 查询DeviceGroup分页
     *
     * @param id     * @param groupCode  组编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<DeviceGroup>
     */
    @Override
    public List<DeviceGroup> list(java.lang.Long id, java.lang.String groupCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        return deviceGroupDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String groupCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        return deviceGroupDAO.count(param);
    }

}
